package com.cynteka.cyfoman.ui;

import com.cynteka.cyfoman.core.config.ActionMD;
import com.cynteka.cyfoman.core.config.ActionsMD;
import com.cynteka.cyfoman.core.config.JAXBLoader;

import javax.swing.*;
import javax.xml.bind.JAXBException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * User: Sergey
 * Date: 03.08.11
 * Time: 13:21
 */
public class MenuLoader extends JAXBLoader {
    private EventDispatcher eventDispatcher;

    public EventDispatcher getEventDispatcher() {
        return eventDispatcher;
    }

    public void setEventDispatcher(EventDispatcher eventDispatcher) {
        this.eventDispatcher = eventDispatcher;
    }

    public JMenu loadMenu(String fileName) throws JAXBException, IOException {
        ActionsMD actions = loadXML(ActionsMD.class, fileName);

        final JMenu menu = new JMenu();

        for (final ActionMD action : actions) {
            JMenuItem menuItem = new JMenuItem(action.getName());
            menu.add(menuItem);
            menuItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    // TODO
                    eventDispatcher.sendEvent(new ActionMDEvent(action));
                }
            });
        }
        return menu;
    }

}
